package org.jeecg.modules.erp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.erp.apithird.entity.SysDepart;
import org.jeecg.modules.erp.base.entity.ErpGoodsFormula;
import org.jeecg.modules.erp.base.entity.ErpGoodsFormulaDetail;
import org.jeecg.modules.erp.base.mapper.ErpGoodsFormulaMapper;
import org.jeecg.modules.erp.base.service.IErpGoodsFormulaDetailService;
import org.jeecg.modules.erp.base.service.IErpGoodsFormulaService;
import org.jeecg.modules.erp.base.vo.ErpGoodsFormulaPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 物料配方
 * @Author: jeecg-boot
 * @Date: 2024-05-22
 * @Version: V1.0
 */
@Service
public class ErpGoodsFormulaServiceImpl extends ServiceImpl<ErpGoodsFormulaMapper, ErpGoodsFormula> implements IErpGoodsFormulaService {

    @Autowired
    private IErpGoodsFormulaDetailService iErpGoodsFormulaDetailService;

    @Override
    public IPage<ErpGoodsFormulaPage> goodsFormulaPage(Page<ErpGoodsFormula> page, ErpGoodsFormula model) {
        return getBaseMapper().goodsFormulaPage(page, model);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void goodsFormulaSave(ErpGoodsFormula body) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        SysDepart depart = getBaseMapper().getUserDepart(sysUser.getOrgCode());
        if (depart != null) {
            body.setSysOrgCode(depart.getOrgCode());
        }
        this.save(body);
        if(!CollectionUtils.isEmpty(body.getDetailList())){
            this.iErpGoodsFormulaDetailService.deleteByFormualCode(body.getCode());
            List<ErpGoodsFormulaDetail> detailList = body.getDetailList();
            detailList.stream().forEach(item->{
                item.setFormulaCode(body.getId());
            });
            this.iErpGoodsFormulaDetailService.saveBatchList(detailList);
        }
    }

    @Override
    public void goodsFormulaEdit(ErpGoodsFormula body) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        SysDepart depart = getBaseMapper().getUserDepart(sysUser.getOrgCode());
        if (depart != null) {
            body.setSysOrgCode(depart.getOrgCode());
        }
        this.updateById(body);
        this.iErpGoodsFormulaDetailService.deleteByFormualCode(body.getId());
        if(!CollectionUtils.isEmpty(body.getDetailList())){
            List<ErpGoodsFormulaDetail> detailList = body.getDetailList();
            detailList.stream().forEach(item->{
                item.setFormulaCode(body.getId());
                this.iErpGoodsFormulaDetailService.save(item);
            });
//            this.iErpGoodsFormulaDetailService.saveBatchList(detailList);
//            this.iErpGoodsFormulaDetailService.saveBatch(detailList);
        }
    }

    @Override
    public ErpGoodsFormula formula(String id) {
        ErpGoodsFormula formual = this.getById(id);
        List<ErpGoodsFormulaDetail> erpGoodsFormulaDetails = this.iErpGoodsFormulaDetailService.listByFormualCode(formual.getCode());
        formual.setDetailList(erpGoodsFormulaDetails);
        return formual;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteFormula(String id) {
        ErpGoodsFormula formula = this.getById(id);
        boolean b = this.removeById(id);
        if(b){
            boolean remove = iErpGoodsFormulaDetailService.remove(new LambdaQueryWrapper<ErpGoodsFormulaDetail>()
                    .eq(ErpGoodsFormulaDetail::getFormulaCode, formula.getId()));
            return remove;
        }
        return b;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean batchDelete(List<String> list) {
        List<ErpGoodsFormula> formulasList = this.listByIds(list);
        List<String> formulasCodeList = formulasList.stream().map(i -> i.getId()).collect(Collectors.toList());
        long count = this.iErpGoodsFormulaDetailService.count(new LambdaQueryWrapper<ErpGoodsFormulaDetail>()
                .in(ErpGoodsFormulaDetail::getFormulaCode, formulasCodeList));
        boolean isRemove = true;
        if(count>0){
             isRemove = this.iErpGoodsFormulaDetailService.remove(new LambdaQueryWrapper<ErpGoodsFormulaDetail>()
                    .in(ErpGoodsFormulaDetail::getFormulaCode, formulasCodeList));
        }
        if(isRemove){
            return this.removeByIds(list);
        }
       return isRemove;
    }
}
